<script lang="ts" setup>
  import { useUserStore } from '/@/store/modules/user'
  import { toLoginRoute } from '/@/utils/routes'
  import { translateTitle } from '/@/utils/i18n'

  const route = useRoute()
  const router = useRouter()

  const userStore = useUserStore()
  const { avatar, username } = storeToRefs(userStore)
  const userInfo: any = localStorage.getItem('userInfo')
  const _userInfo = JSON.parse(userInfo)
  const { logout } = userStore

  const active = ref(false)

  const handleVisibleChange = (value: boolean) => {
    active.value = value
  }
  const handleCommand = async (command: any) => {
    switch (command) {
      case 'logout':
        await logout()
        await router.push('/index')
        // toLoginRoute(route.fullPath)
        break
      case 'personalCenter':
        await router.push('/setting/personalCenter')
        break
    }
  }
</script>

<template>
  <el-dropdown @command="handleCommand" @visible-change="handleVisibleChange">
    <span class="avatar-dropdown">
      <el-avatar class="user-avatar" :src="avatar" />
      <div class="username">
        <span class="hidden-xs-only">{{ _userInfo.name || '' }}</span>
        <vab-icon
          class="vab-dropdown"
          :class="{ 'vab-dropdown-active': active }"
          icon="arrow-down-s-line"
        />
      </div>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="logout">
          <vab-icon icon="logout-circle-r-line" />
          <span>{{ translateTitle('退出登录') }}</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<style lang="scss" scoped>
  .avatar-dropdown {
    display: flex;
    align-content: center;
    align-items: center;
    justify-content: center;
    justify-items: center;

    .user-avatar {
      box-sizing: border-box;
      width: 40px;
      height: 40px;
      padding: 8px;
      margin-left: 15px;
      cursor: pointer;
      border-radius: 50%;
    }

    .username {
      position: relative;
      display: flex;
      align-content: center;
      align-items: center;
      height: 40px;
      margin-left: 6px;
      line-height: 40px;
      cursor: pointer;

      [class*='ri-'] {
        margin-left: 0 !important;
      }
    }
  }
</style>
